<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="姓名">
              <a-input v-model="queryParam.keyword" placeholder @keydown.enter="getDataList" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="() => {
              this.pagination.current = 1
              this.getDataList()
            }">查询
            </a-button>
            <a-button style="margin-left: 8px" @click="() => (queryParam = {Condition:'Name'})">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <a-table ref="table" :scroll="{ y: tableHeight }" :columns="columns" :rowKey="(row, index) => row.Id"
      :dataSource="data" :pagination="pagination" :loading="loading" @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }" :bordered="true" size="small">
      <span slot="UserAvatarURL" slot-scope="text, record">
        <a v-if="hasPerm('Base_User.Edit')">
          <a-popover>
            <!-- <img :src="text" @error="this.src = null" width="30px" height="30px" /> -->
            <a-avatar icon="user" :src="text" style="width: 30px;height: 30px;" />
            <template #content>
              <a-avatar icon="user" :src="text" :size="200" style="width: 200px;height: 200px;" />
            </template>
          </a-popover>
        </a>
        <a v-if="!hasPerm('Base_User.Edit')">
          <a-popover>
            <!-- <img :src="text" @error="this.src = null" width="30px" height="30px" /> -->
            <a-avatar icon="user" :src="text" style="width: 30px;height: 30px;" />
            <template #content>
              <a-avatar icon="user" :src="text" :size="200" style="width: 200px;height: 200px;" />
            </template>
          </a-popover>
        </a>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <template v-if="hasPerm('Base_User.Edit')">
            <a @click="handleEdit(record.Id)">查看</a><a-divider type="vertical" />
            <a @click="handleContacts(record.Id)">通讯录</a>
          </template>
        </template>
      </span>
    </a-table>
    <edit-form ref="editForm" :afterSubmit="getDataList"></edit-form>
    <contacts :pId="data.Id" ref="contacts"></contacts>
  </a-card>
</template>

<script>
import EditForm from './EditForm'  //查看
import Contacts from './Contacts' //通讯录
const columns = [
  { title: '头像', dataIndex: 'Icon', width: '55px', scopedSlots: { customRender: 'UserAvatarURL' }},
  { title: '姓名', dataIndex: 'Name', width: '12%' },
  { title: '性别', dataIndex: 'Sex', width: '8%' },
  { title: '手机号码', dataIndex: 'Phone', width: '20%' },
  { title: '地址', dataIndex: 'Address', width: '20%' },
  { title: '邮箱', dataIndex: 'Email', width: '20%' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' }, width: '12%' }
]

export default {
  components: {
    EditForm,
    Contacts
  },
  mounted() {
    this.$nextTick(() => {
      // 页面渲染完成后的回调
      this.tableHeight = this.getTableHeight(true) + 24
    })
    this.getDataList()
  },
  data() {
    return {
      tableHeight: 570, // 表格高度
      data: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面啊可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'CreateTime', order: 'desc' },
      loading: false,
      columns,
      queryParam: {Condition:'Name'},
      visible: false,
      selectedRowKeys: [],
      imgUrl: require('@/assets/logo.png')
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []
      this.loading = true
      this.$http
        .post("/Base_Manage/Base_VIPUserInfo/GetDataList", {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    handleEdit(id) {
      this.$refs.editForm.openForm(id, '查看用户信息')
    },
    handleContacts(id) {
      this.$refs.contacts.getDataList(id, '查看通讯录信息')
    },

    imgError(event) {
      const img = event.src
      img.src = null
      img.onerror = null // 防止闪图
      // return this.imgUrl
      // const img = event.src
      // img.src = this.imgUrl
      // img.onerror = null // 防止闪图
    },

  }
}
</script>
